package me.fangx.zhihu.ui.activity;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.fangx.common.ui.activity.BaseToolBarActivity;
import me.fangx.common.util.eventbus.EventCenter;
import me.fangx.common.util.log.LogUtil;
import me.fangx.common.util.netstatus.NetUtils;
import me.fangx.zhihu.CookBookApp;
import me.fangx.zhihu.R;
import me.fangx.zhihu.modle.bean.CookBean;
import me.fangx.zhihu.modle.dao.DBManager;
import me.fangx.zhihu.utils.BaseUtil;
import me.fangx.zhihu.utils.NightModeHelper;

public class CookDetailActivity extends BaseToolBarActivity {

    public static final String ARG_ITEM_INFO = "item_info";
    private CookBean cookBean;
    private int position = 0;

    @Bind(R.id.appbar)
    AppBarLayout appbar;

    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.tv_material)
    TextView tv_material;

    @Bind(R.id.tv_practice)
    TextView tv_practice;

    @Bind(R.id.iv_cook_picture)
    SimpleDraweeView iv_cook_picture;

    @Bind(R.id.collapsing_toolbar)
    CollapsingToolbarLayout collapsingToolbar;

    @Bind(R.id.fab)
    FloatingActionButton fab;
    //日夜模式切换
    private NightModeHelper nightModeHelper;

    @Override
    protected void getBundleExtras(Bundle extras) {
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_detail;
    }

    @Override
    protected void initViewsAndEvents() {
        cookBean = (CookBean) getIntent().getSerializableExtra(ARG_ITEM_INFO);
        position = getIntent().getIntExtra("position", 0);
        if (!providesActivityToolbar()) {
            setToolbar(toolbar);
        }

        if (cookBean != null) {
            loadBackdrop();
            collapsingToolbar.setTitle(cookBean.getName());
            tv_material.setText(cookBean.getMaterial().replace("\\n", "\n\n"));
            tv_practice.setText(cookBean.getPractice().replace("\\n", "\n\n"));
            fab.setSelected(cookBean.getIsCollect()==0?false:true);
        }

        // AppBar的监听
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int maxScroll = appBarLayout.getTotalScrollRange();
                float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;
                int currentMode = (CookBookApp.getContext().getResources().getConfiguration()
                        .uiMode & Configuration.UI_MODE_NIGHT_MASK);
                LogUtil.d("cuttentMode"+currentMode + "");
                if (currentMode == Configuration.UI_MODE_NIGHT_YES) {
                    toolbar.setBackgroundColor(Color.argb((int) (percentage * 255), 33, 43, 48));
                } else {
                    toolbar.setBackgroundColor(Color.argb((int) (percentage * 255), 255, 76, 56));
                }

            }
        });
    }

    @OnClick(R.id.fab)
    void clickFab(){
        new DBManager().collectCook(cookBean.getId(), cookBean.getIsCollect()==0?1:0);
        cookBean.setIsCollect(cookBean.getIsCollect()==0?1:0);
        fab.setSelected(cookBean.getIsCollect()==0?false:true);
        Intent intent = new Intent("com.blue.recipe.collect");
        intent.putExtra("position", position);
        intent.putExtra("cookbean", cookBean);
        sendBroadcast(intent);
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {

    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    private void loadBackdrop() {
        if (cookBean.getPictureId() != -1) {
            iv_cook_picture.setBackgroundResource(BaseUtil.getDrawableId(cookBean.getType(), cookBean.getPictureId()));
        }
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


    @Override
    public boolean providesActivityToolbar() {
        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
